package com.cy.train.member.service.imp;

import cn.hutool.core.bean.BeanUtil;
import com.cy.train.common.exception.BusinessException;
import com.cy.train.common.exception.BusinessExceptionEnum;
import com.cy.train.common.util.JwtUtil;
import com.cy.train.common.util.SnowUtil;
import com.cy.train.member.domain.Member;
import com.cy.train.member.domain.MemberExample;
import com.cy.train.member.mapper.MemberMapper;
import com.cy.train.member.req.MemberLoginReq;
import com.cy.train.member.req.MemberRegisterReq;
import com.cy.train.member.req.MemberSendCodeReq;
import com.cy.train.member.resp.MemberLoginResp;
import com.cy.train.member.service.MemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Date: 2024/2/26 7:57
 * @Author: cyzhang
 */
@Service
public class MemberServiceImp implements MemberService {
    private static final Logger LOG = LoggerFactory.getLogger(MemberServiceImp.class);

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Long count() {
        return memberMapper.countByExample(null);
    }

    @Override
    public Long register(MemberRegisterReq req) {
        String mobile = req.getMobile();
        Member Member = selectMemberByMobile(mobile);
        if(Member != null) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        } else {
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
            return member.getId();
        }
    }

    @Override
    public String sendCode(MemberSendCodeReq req) {
        String mobile = req.getMobile();

        //查询手机号是否存在
        Member Member = selectMemberByMobile(mobile);
        if(Member == null) {
            //手机号不存在，向member表中增加一条记录
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
            LOG.info("手机号不存在，向member表中增加一条记录");
        } else {
            LOG.info("手机号存在，直接获取验证码");
        }

        //String code = RandomUtil.randomString(4);
        String code = "6666";
        LOG.info("验证码为：{}", code);

        //短信记录表：手机号，验证码，有效期，是否使用，使用类型，发送时间，使用时间
        LOG.info("向短信记录表中增加一条记录");

        LOG.info("对接短信服务通道");

        return code;
    }

    @Override
    public MemberLoginResp login(MemberLoginReq req) {
        String mobile = req.getMobile();
        String code = req.getCode();
        LOG.info("用户输入验证码为：{}", code);
        //查询手机号是否存在
        Member member = selectMemberByMobile(mobile);
        if(member == null) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        if(code.equals("6666")) {
            MemberLoginResp memberLoginResp = BeanUtil.copyProperties(member, MemberLoginResp.class);
            Map<String, Object> payload = BeanUtil.beanToMap(memberLoginResp);
            String token = JwtUtil.createToken(payload);
            memberLoginResp.setToken(token);
            return memberLoginResp;
        } else {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }
    }

    private Member selectMemberByMobile(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> memberList = memberMapper.selectByExample(memberExample);
        return memberList.isEmpty() ? null : memberList.get(0);
    }
}
